<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="org.apache.naming.java.javaURLContextFactory"%>
<%@page import="java.util.Date,java.util.List,com.rbcs.reimb.model.*"%>
<%@page import="com.rbcs.base.company.CompanyHelp" %>  
<%@page import="com.rbcs.RbcsConstants" %> 
<%@include file="/common/taglibs.jsp"%>
<html>
<head>
<%@include file="/common/extjs.jsp" %>
<%@include file="/common/meta.jsp"%>
<%@include file="/common/validator.jsp"%>
<link href="${ctx}/styles/treeSelect.css" type='text/css' rel='stylesheet'>
<title>报销申请管理</title>
</head>
<body>
<div class="x-panel" style="width: 100%">
<div class="x-panel-header">报销申请管理</div>
<div><%@ include file="/common/messages.jsp"%></div>
<div align="center" style="width: 100%">
<s:form action="saveChild.do" id="saveChild" method="post">
	<s:hidden name="model.id" id="id"/>
    <s:hidden name="parentReimbId" id="parentReimbId"/>
	<table width="95%" border="1" align="center" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-left-width:0px; border-top-width:0px; padding: 4px 2px; margin-top: 10px;">
		<tr height="30" style="padding: 8px 2px;"> 
           <td align="center" colspan="6"><font size="3">报销单</font></td>
        </tr>	
        <tr height="30">
           <td align="right" width="130">报销单号：</td>
           <td align="left"  width="260">
           		<s:textfield id="singleNo" name="model.singleNo" cssClass="required" cssStyle="width:140px; border:1px dotted #808080;"></s:textfield >
           		<font color="red">*</font>
			</td>
           <td align="right" width="130">部门名称：</td>
           <td align="left" width="230">
               <div id='comboxWithTree'></div>   
               <input id="nodeText" style="width: 248px;display: none;" disabled="disabled"/>
               <input type="hidden" id="parentDeptId" name="parentDeptId" class="dept" value="${model.dept.id}"/>     
           </td>	 			
           <td align="right" width="80">制单日期：</td>
           <td align="left"  width="180">&nbsp;<s:date name="model.createDate" format="yyyy-MM-dd HH:mm"/></td>		
        </tr>         
 
         <tr height="30">
           <td align="right" width="100">项目类别：</td>
           <td align="left" >
           	  <select id="typeId" name="projectTypeId" style="width: 140px;">
           	     <option value="0">&nbsp;请选择...</option>
			     <s:iterator value="projectTypes" var="type">
			       <s:if test="model.project.projectType.id == #type.id">
			         <option value="${type.id}" selected="selected">&nbsp;${type.code}&nbsp;-&nbsp;${type.name}&nbsp;&nbsp;&nbsp;</option>
			       </s:if>
			       <s:else>
			       	 <option value="${type.id}">&nbsp;${type.code}&nbsp;-&nbsp;${type.name}&nbsp;&nbsp;&nbsp;</option>
			       </s:else>
			     </s:iterator>
			   </select>
			</td>
           <td align="right" width="100">项目名称：</td>
           <td align="left" colspan="3">
                <input type="hidden" id="projectId" name="projectId" value="${model.project.id}">
                <input type="text" id="projectName" value="${model.project.name}" onclick="openSelect()" style="width:455px; border:1px dotted #808080;">
           </td>	 				
        </tr>  
               	    	
	</table>

	<table align="center" width="96%"> 
	<tr><td><div align="left"><input name="button" type="button" value="添加明细" class="button"  onclick="addRow()" style="width: 70px;"></div></td></tr>
	</table>
	
<table width="95%" border="1" align="center" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-left-width:0px; border-top-width:0px; padding: 1px 1px; ">
	  <tbody id="tbody">
	    <tr bgcolor="#F3F4F3" align="center" height="30">
	       <td style="width:180px;">科目名称</td>
	       <td style="width:260px;">摘要</td>
	       <td style="width:100px;">金额</td>
	       <td style="width:100px;">外币金额</td>	
	       <td style="width:100px;">外币币种</td>	   	              	       	       
	       <td style="width:80px;">操作</td>
	    </tr>
	    <tr style="display:none" bgcolor="#FFFFFF"> 
	      <td align="center"><input name="itemName" type="text" onclick="checkSelectItem(this.parentNode.parentNode)" style="width:176px;border:1px dotted #808080;" title="请您选择科目" readonly="readonly"/>
	      <input type="hidden" name="itemId" id="itemId"/></td>
	      <td align="center"><input name="title" type="text" style="width:256px;border:1px dotted #808080;" title="请您填写摘要"/></td>
	      <td align="center"><input name="amount" type="text" onkeyup="yzsz(this, this.parentNode.parentNode)" style="width:96px;border:1px dotted #808080;" title="请您输入报销金额"/></td>
	      <td align="center"><input name="exchangeAmount" type="text" onkeyup="yzwbsz(this, this.parentNode.parentNode)" style="width:96px;border:1px dotted #808080;" title="请您输入报销外币金额"/></td>	      	      
	      <td align="center">
	      <input type="hidden" id="cRate"/>				  
	      <input name="cName" type="text" style="width:96px;border:1px dotted #808080;" onclick="selectCurrencys(this.parentNode.parentNode)" title="请您选择外币币种种类" />
	      <input type="hidden" id="cId" name="cId" />
	      </td> 	 
	      <td align="center"><input type="button" value="删除" onclick="removeRow(this.parentNode.parentNode)" class="button" ></td>
	    </tr>
	    
	   <s:if test="reimbDetail.size==0">
	   </s:if>
	   <%
	   List<ReimbDetail> reimbDetails = (List<ReimbDetail>)request.getAttribute("reimbDetail");
	   if(reimbDetails != null){
	   for(ReimbDetail rd : reimbDetails){
	   %>
	   <tr height="30" bgcolor="#FFFFFF"> 
	      <td align="center"><input name="itemName" type="text" value="<%=rd.getItem().getName()%>【<%=rd.getItem().getCode()%>】" onclick="checkSelectItem(this.parentNode.parentNode)" style="width:176px;border:1px dotted #808080;" title="请您选择科目" readonly="readonly"/>
	      <input type="hidden" name="itemId" value="<%=rd.getItem().getId()%>"/></td>	   
	      <td align="center"><input name="title" type="text" value="<%=rd.getTitle()%>" style="width:256px;border:1px dotted #808080;" title="请您填写摘要"/></td>
	      <td align="center"><input name="amount" value="<%=rd.getAmount()%>" type="text" onkeyup="yzsz(this)" style="width:96px;border:1px dotted #808080;" title="请您输入报销金额"/></td>
	      <td align="center"><input name="exchangeAmount" value="<%=rd.getExchangeAmount()%>" type="text" onkeyup="yzwbsz(this, this.parentNode.parentNode)" style="width:96px;border:1px dotted #808080;" title="请您输入报销外币金额"/></td>
	      <td align="center">
	      <c:set var="currency" value="<%=rd.getCurrency()%>" scope="request"/>
	      <s:if test="#attr.currency != null">
	         <input name="cName" type="text" value="<%=rd.getCurrency().getName()%>" style="width:96px;border:1px dotted #808080;" onclick="selectCurrencys(this.parentNode.parentNode)" title="请您选择外币币种种类" />
	         <input type="hidden" name="cId" id="cId" value="<%=rd.getCurrency().getId()%>"/>
	      </s:if>
	      <s:else>
		     <input name="cName" type="text" style="width:96px;border:1px dotted #808080;" onclick="selectCurrencys(this.parentNode.parentNode)" title="请您选择外币币种种类" />
	         <input type="hidden" name="cId" id="cId"/>      
	      </s:else>
	      </td> 	      	      	      
	      <td align="center"><input type="button" value="删除" onclick="removeRow(this.parentNode.parentNode)" class="button" ></td>	       
	    </tr>
	    <%}} %>	    	     
	    </tbody>
		
		<tr bgcolor="#FFFFFF" height="30"> 
	       <td colspan="2" align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	       <input type="text" value="报销金额合计："  readonly="readonly" style="width:100px;padding-top: 5px;border: 0;"/>
	       <td align="left"><input id="reimbTotal" name="model.amount" type="text" readonly="readonly" style="width:96px;padding-top: 5px;border: 0;"/></td>
	       <td align="left">&nbsp;</td>	       	       	       
	       <td align="left">&nbsp;</td>
	       <td align="left">&nbsp;</td>
	    </tr> 
	</table>
    <br>
	<table width="600px" style="margin-bottom: 10px;">
		<tr>
			<td style="text-align: center;">
              <input type="button" class="button" value="保存" style="font-weight:bold;" onclick="saveAjax()">
			  &nbsp;&nbsp;&nbsp;&nbsp;
			  <!--  
			  ${model.id} ${parentReimbId}
			  <s:submit value="保存" cssClass="button" onclick="return saveAjax();"/> 
			  &nbsp;&nbsp;
			  <s:reset value="重置" cssClass="button" />
			  &nbsp;&nbsp;
			  <s:reset value="返回" cssClass="button" onclick="history.go(-1)"/>
			  -->
			  <input type="button" class="button" value="关闭" style="font-weight:bold;" onclick="javascript:window.close();">
			</td>
		</tr>
	</table>
</s:form>
</div>
</div>
<script type="text/javascript" src="${ctx }/pages/rbcs/reimb/editChildjs.js"></script>
<script type="text/javascript" src="${ctx}/pages/admin/dept/edit.js"></script>
<script type="text/javascript">
var rootName = '<%=CompanyHelp.getComName()%>';
var initValue = '${model.dept.name}';
if (initValue.length == 0){
	initValue = rootName;
}
Ext.onReady(function() {
	var dtree = new DeptTree({
		url : '/admin/dept/deptTree.do',
		rootName: rootName,
		initValue : initValue,
		el : 'comboxWithTree',
		innerTree :'inner-tree',
		onclick : function(nodeId, nodeText) {
			 document.getElementById("nodeText").value = nodeText;	 
			 Ext.get('parentDeptId').dom.value = nodeId; 
		}
	});
	dtree.init();	
});

$(document).ready(function() {
	var itemName = document.getElementsByName("itemName") ;
	if(itemName.length > 1){
		//document.getElementById("comboxWithTree").style.display = "none";
		//document.getElementById('comboxWithTree').readOnly=true; 
		
		document.getElementById("comboxWithTree").style.display = "none";
		document.getElementById("nodeText").style.display = "";
		document.getElementById("nodeText").value = '${model.dept.name}';
		document.getElementById('projectName').disabled = true; 
	 }
	zjs(); 
});

/**
 * 根据项目类别选择项目
 */
function openSelect() {
	var typeId = document.getElementById("typeId").value ;
	if (typeId > 0 ) {
	   var url = '${ctx}/base/project/selectAllProject.do?projectTypeId=' + typeId;		
	} else {
	   var url = '${ctx}/base/project/selectAllProject.do';	
	}
	var styles = "dialogWidth:502px; dialogHeight:365px; status:no; scroll:no; help:no";
	var project = window.showModalDialog(url, null, styles);
	if (project != null){
    	document.getElementById("projectId").value = project.id;
    	document.getElementById("projectName").value = project.name;
	} else{
    	document.getElementById("projectId").value = "";
    	document.getElementById("projectName").value = "";
	}
	var tab = document.getElementsByName("itemName") ;
	for(var i = 1; i < tab.length; i++){
  	  tab[i].value = "";
  	}
}

/**
 * 判断是部门科目还是项目科目
 */
function checkSelectItem(r) {
	var projectId = document.getElementById("projectId").value;
	var deptId = document.getElementById("parentDeptId").value;
	if (projectId != null && projectId != "") {
		openSelectItem(r, projectId, "project");
	}else if (deptId != "" && deptId != 0) {
		openSelectItem(r, deptId, "dept");
	}else {
		alert("请您选择部门名称或者项目名称！");
		return false;
	}
}

/**
 * 选择科目
 */
function openSelectItem(r, id, type){
	var url = null;
	if(type == "dept"){
		url = '${ctx}/base/item/selectItem.do?dept.id=' + id + '&inputType=radio';
	}else{
		url = '${ctx}/base/item/selectItem.do?project.id=' + id + '&inputType=radio';
	}
	var styles = "dialogWidth:630px; dialogHeight:400px; status:no; scroll:no; help:no";
	var item = window.showModalDialog(url, null, styles);
	if (!item){
		//do nothing...
	} else{
		var tab1 = document.getElementsByName("itemId") ;
		tab1[r.rowIndex-1].value = item.id;		
		var tab2 = document.getElementsByName("itemName") ;
		tab2[r.rowIndex-1].value = item.name + "【" + item.code + "】";
		
		if (tab1[r.rowIndex-1].value != null) {
			document.getElementById("comboxWithTree").style.display = "none";
			document.getElementById("nodeText").style.display = "";
			document.getElementById('projectName').disabled = true; 
		}
	     
		if(item.name == '<%=RbcsConstants.ITEM_NAME_TRAVEL%>') {
		   var tab3 = document.getElementsByName("title");
		   var url = '${ctx}/base/tripgrade/selectTripGrade.do';
		   var styles = "dialogWidth:502px; dialogHeight:385px; status:no; scroll:no; help:no";
		   var tripGrade = window.showModalDialog(url, null, styles);		   
		   tab3[r.rowIndex-1].value = tripGrade.name + "【" + tripGrade.subsidy + "元/天】";
		}
	}
}
 
/**
 * 异步保存报销子单信息
 */
function saveAjax(){
	 var tab = document.getElementsByName("itemName") ;
	 var tab1 = document.getElementsByName("amount") ;
	 if(tab.length == 1){
		alert("科目不能为空，请选择科目！");
		return false;
	  }
	 for(var i=1; i<tab.length; i++){
	    if(tab[i] == null || tab[i].value == ""){
		   alert("科目不能为空，请选择科目！");
		   return false;
		} 
	  }
	 for(var i=1; i<tab1.length; i++){
		if(tab1[i].value == "" || tab1[i].value - 0 == 0 ){
		   alert("请输入金额，金额不能为0！");
		   return false;
		}
	  }
	 
	 var id = document.getElementById('id').value;
	 var singleNo = document.getElementById('singleNo').value;
	 var parentReimbId = document.getElementById('parentReimbId').value;
	 var parentDeptId = document.getElementById('parentDeptId').value;
	 var projectId = document.getElementById('projectId').value;
	 var reimbTotal = document.getElementById('reimbTotal').value;
	 
	 var itemId = document.getElementsByName("itemId");
	 var itemIds = "";
	 for(var i = 1; i < itemId.length; i++){
		 itemIds = itemIds + itemId[i].value + ",";
      }
	 
	 var title = document.getElementsByName("title");
	 var titles = "";
	 for(var i = 1; i < itemId.length; i++){
		 titles = titles + title[i].value + " ,";
      }
	 
	 var amount = document.getElementsByName("amount");
	 var amounts = "";
	 for(var i = 1; i < amount.length; i++){
		 amounts = amounts + amount[i].value + ",";
     }
	 
	 var exchangeAmount = document.getElementsByName("exchangeAmount");
	 var exchangeAmounts = "";
	 for(var i = 1; i < exchangeAmount.length; i++){
		 exchangeAmounts = exchangeAmounts + exchangeAmount[i].value + " ,";
     }
	 
	 var cId = document.getElementsByName("cId");
	 var cIds = "";
	 for(var i = 1; i < cId.length; i++){
		 cIds = cIds + cId[i].value.split(",")[0] + " ,";
     }
	 
	 if(confirm('您确定修改报销申请信息吗？')){
  	     $.ajax({
 			url: '${ctx}/reimb/saveChildAjax.do',
 			type: 'post',
 			dataType: 'json',
 			data: {
 				id : id, 
 				singleNo : singleNo,
 				parentReimbId : parentReimbId, 
 				parentDeptId : parentDeptId,
 				projectId : projectId,
 				reimbTotal : reimbTotal,
 				itemIds : itemIds,
 				titles  : titles,
 				amounts : amounts,
 				exchangeAmounts : exchangeAmounts,
 				cIds    :  cIds
 			},
 			success: function(rst, textStatus){
 	  		  if(rst.result == "success"){   
 	  			var reimbChild = new Object();
 	  			reimbChild.flag = "success";
 	  			window.returnValue = reimbChild;
 	  			window.close();
 	  	  	  }
 			}
 	  	 });       
	 }
}
</script>
</body>
</html>